<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/commons/include/html_doctype.html"%>
<%@include file="/commons/page/codegen.jsp"%>
<html>


<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <%@include file="/commons/page/codegen.jsp"%>
    <%-- <link href="${ctx}/js/gpoint/rdp/zone/css/bootstrap.min.css" rel="stylesheet" /> 
    <link href="${ctx}/js/gpoint/rdp/zone/css/bootstrap-duallistbox.min.css" rel="stylesheet" />
    
    <script src="${ctx}/js/gpoint/rdp/zone/jquery-3.0.0.min.js"></script>
    <script src="${ctx}/js/gpoint/rdp/zone/bootstrap.min.js"></script>
    <script src="${ctx}/js/gpoint/rdp/zone/jquery.bootstrap-duallistbox.js"></script>
    <link type="text/css" rel="stylesheet" href="${ctx}/js/gpoint/rdp/zone/css/bootstrap.css">

	<script type="text/javascript" src="${ctx}/js/gpoint/rdp/zone/bootstrap-paginator.js"></script>
	<script type="text/javascript" src="${ctx}/js/gpoint/rdp/TimeSegment/timeSeg.js"></script>
--%>
	<style type="text/css">
	
		#mermbers {
			width: 98%;
		    margin: auto;
		    margin-top:15px;
		}
		
		.myButton{
		    BORDER: 1PX SOLID;
		    PADDING: 2PX;
		    BACKGROUND-COLOR: #337ab7;
		    COLOR: WHITE;
		    BORDER-RADIUS: 5PX;
		    height: 27px;
		}
		
		.myLink{
		    BORDER: 1PX SOLID;
		    PADDING: 5PX;
		    BACKGROUND-COLOR: #337ab7;
		    COLOR: WHITE;
		    BORDER-RADIUS: 5PX;
		}
	</style>
</head>

<body>



	<div class="panel panel-default">
	    <div class="panel-heading">
	      <!--   不带 title 的面板标题 --> 权限类型选择
	    </div>
	    <div class="panel-body">
	        <!--    面板内容 -->
		   <div class="toolbar-body" >		      
				<form role="form" class="search-form">
				  
					<div  class="form-inline p-xxs">	
					    <div class="form-group">
								  权限类型<input type="text" id="permissionProperty" value="${type}" readonly="readonly"> 

								  
						</div>
							 <div class="form-group">
								     时间段<select id="datasetProperty" onchange="cge()">  </select>
						</div>	
										
						<div class="form-group">
								 <a id="showValue3" class="btn btn-primary fa fa-search" href="javascript:void(0);" ><span>允许</span></a>
								<!--  <a id="showValue4" class="btn btn-primary fa fa-search" href="javascript:void(0);" ><span>取消</span></a> -->
						</div>
														
					</div>
				</form>
			 </div><!--/ 查询条件-->
	    </div>
	</div>
 
	<ul id="myTab" class="nav nav-tabs">
		<li class="active">
		   <a href="#home" data-toggle="tab">菜鸟教程</a>
		</li>
	    <li><a href="#ios" data-toggle="tab">门列表</a></li>
	    <li class="dropdown"></li>
	</ul>
	<div id="myTabContent" class="tab-content">
	
		<div class="tab-pane fade in active" id="home">
			 <div class="wrapper wrapper-content  animated fadeInRight col-sm-12">    	
					<div class="jqGrid_wrapper" id="mermbers" >		
						<div>
						   <label class="fr-control-label"></label>
							<div class="fr-form-group">
								<input class="myButton" type = "button" value = "选择名单"  onclick="selectMember();">
		
								<a class="myLink" href="javascript:void(0);" onClick="downloadExcel();">模板下载</a>
								<a class="myLink" href="javascript:void(0);" onClick="fileClick()">批量导入</a>
								<input type="file" id="upfile" name="upfile" placeholder="" οnchange="importExp();" style="display: none;"/>
						
							</div>
						</div>	
						<table id="campusCardGrid" ></table>
						<div id="campusCardPager"></div>
					</div>
				</div>
		</div>
	
		<div class="tab-pane fade" id="ios">
		    <!--  门列表 -->
	
		</div>
    </div>
    <script type="text/javascript">





function appendHtml2(returnData) {//此函数用于处理后台返回的数据，根据自己需求来实现页面拼接
	  var objs = $.parseJSON( JSON.stringify(returnData));
	 // alert("aaa");

	  if(objs==null ){
		  alert("门数据获取异常！");
		   return;
		  
	  }else{
		  $(objs).each(function () {
		       
		         var objA=objs.rows;    
		         
		         var json = [];
               
               $(objA).each(function () {
            	   var o = document.createElement("option");
                   
                   
                   var label = this['key'];
	                
	                console.log(label);
	                
	                
                  var value = this['doorno']+","+this['doorname'];
              
                  var  flag=this['selectedflag'];
                 
                  if(!flag){ //flase 
                  
                   $('.demo2').append('<option value="' + value + '">' + label + '</option>');
                  }else{
               	   $('.demo2').append('<option value="' + value +'" selected="selected">' +label + '</option>');
                  }
                  
               });
               
              
	            
		       
		         $('.demo2').bootstrapDualListbox('refresh');
		       
		    });      //渲染dualListbox
		    $('.' + 'demo2' + '').bootstrapDualListbox({
		    	  nonSelectedListLabel: '门列表',
		          selectedListLabel: '已选择门数：',
		          preserveSelectionOnMove: 'moved',
		          moveOnSelect: false//,
		    });
	  }
 
  
}




	
	
var doornum=0;
function a(caruid){
	  var strs = new Array(); //定义一数组
	  
	   strs =  String(caruid).split(","); //字符分割

	   doornum=strs.length/2;
	//   alert("获取门的总数集合"+doornum);
	   
	   var b=[];
	   for (i=0; i<strs.length; i++ ){
	        
		   if(i%2==0){  
			   if(i==0){
				  
				   b.push(String (strs[i])+","+strs[i+1]+"|");
			   }else{
				 
				  
				   b.push(String (strs[i])+","+strs[i+1]+"|");
			   }
			   
		   }
	   }
	   console.log(b);
	   return b;
	  //end
}



	
	var selectvalue;

/* 
	var index; //时间框控件选择数值
	var selectvalue; */
$("#showValue3").click(function () {
	//alert("授权进行");
	//得到第一个列表
       
	
	   
	//  alert(b);
	//得到门列表
	  var doorlist=$('[name="duallistbox_demo2"]').val(); 
	  
	//  alert("doorlist="+doorlist);
	   c= a(doorlist);
	//  alert(c);
	  
	 
	//  alert(selectvalue);
	  
	  
	  //得到下拉框的数值
	 var permissonselect=$("#permissionProperty").val();    
	  
	  
	//1.判断为空，必须填完才能进行保存，不能进行类型授权操作：
		if(doorlist.length!=0){

			 //拼成JSON数组传递
			   var str = [];
				    		 
				    		  // var doorArr = ["111","222"]; //控制器SN号，门号
				    		   var doorArr = c; //控制器SN号，门号
				    		   
				   //3.用户权限
				    		  var map ={};
				    		
		    		    	   map.startTime=permissonselect;   //用户选择类型
		    		    	   
				    	
		 
		    
		    		    	         	
		    		    	     map.doorArr = doorArr; //门列表 和时间段
		    		    	     map.endTime=selectvalue; //门的类型
		    		    	     
		    		    	     
		    		    	     
		    		    	     
		    		    	         	 
		    	//4.存入doorpremisson
		    	    
				    	       $.ajax({
	    		    	         	type: "post",
	    		    	         	   url: __ctx+"/rdp/doorPermission/doorPermissonType/query",
	    		    	         	    contentType: "application/json;charset=utf-8;",
	    		    	         	  data:JSON.stringify(map),
	    		    	         	  dataType:"json",//注意不是datatype哦
	    		    	         	    async: false,//使用同步操作，否则init方法执行时setting可能还未被赋值
	    		    	         	   success: function(data) {
	    		    	         		  console.log(data);
	    		    	         		  
	    		    	         	          alert("查询门信息成功！");
	    		    	         	 
	    		    	         	   if(data.result==0){ //不存在

	    		    	         		   $.ajax({
			    		    	         	type: "post",
			    		    	         	   url: _ctx+"/rdp/doorPermission/doorPermissonType/save3",
			    		    	         	    contentType: "application/json;charset=utf-8;",
			    		    	         	  data:JSON.stringify(map),
			    		    	         	  dataType:"json",//注意不是datatype哦
			    		    	         	    async: false,//使用同步操作，否则init方法执行时setting可能还未被赋值
			    		    	         	   success: function(data) {
			    		    	         	          alert("绑定门权限类型成功！");
			    		    	         	  },
			    		    	         	 error: function(){
			    		    	         		 alert("绑定门权限类型异常！");
			    		    	              }
			    		    	         	 
			    		    	         	 }); 
			    		    	         	 //end 
	    		    	         	   }else		   if(data.result==1){//存在
	    		    	         		  //提示信息
	    		    	         	        DialogUtil.confirm(data.message + ',是否继续操作',
												function(rtn) {
												if(rtn){
													window.location.reload(true);
															   //更新入库
															     $.ajax({
						    		    	         	type: "post",
						    		    	         	   url: __ctx+"/rdp/doorPermission/doorPermissonType/update",
						    		    	         	    contentType: "application/json;charset=utf-8;",
						    		    	         	  data:JSON.stringify(map),
						    		    	         	  dataType:"json",//注意不是datatype哦
						    		    	         	    async: false,//使用同步操作，否则init方法执行时setting可能还未被赋值
						    		    	         	   success: function(data) {
						    		    	         	         alert("绑定门权限类型成功！");
						    		    	         	  },
						    		    	         	 error: function(){
						    		    	         		 alert("绑定门权限类型异常！");
						    		    	              }
						    		    	         	 
						    		    	         	 }); 
															     //end
												}   
												else{
													
												}
											    	
												});
	    		    	         	   }else{
	    		    	         		   
	    		    	         	   }
	    		    	         	 
	    		    	         	       
	    		    	         	        
	    		    	         	        
	    		    	         	  },
	    		    	         	 error: function(){
	    		    	         		  alert("查询门信息失败！");
	    		    	              }
	    		    	         	 
	    		    	         	 }); 
	    		    	         	 //end
	    		    	         	 
		    	 /* $.ajax({
		    		    	         	type: "post",
		    		    	         	   url: _ctx+"/rdp/doorPermission/doorPermissonType/save3",
		    		    	         	    contentType: "application/json;charset=utf-8;",
		    		    	         	  data:JSON.stringify(map),
		    		    	         	  dataType:"json",//注意不是datatype哦
		    		    	         	    async: false,//使用同步操作，否则init方法执行时setting可能还未被赋值
		    		    	         	   success: function(data) {
		    		    	         	          alert("绑定门权限类型成功！");
		    		    	         	  },
		    		    	         	 error: function(){
		    		    	         		  alert("绑定门权限类型失败！");
		    		    	              }
		    		    	         	 
		    		    	         	 }); 
		    		    	         	 //end */
		    		    	         	 
		    		    
		    		    	         	 
		    		    	         	 
		    		    	         	 
		    		    	         	 
		    		    	         	 
		    		    	         	 
		    		    	         	 
		    		    	         	 
		    		  //  alert("更新门数");
		    		    	         	 
		    		    	         	 
                     
                   var   id=$("#permissionProperty").val();
		    		    	         	 
                       $.ajax({
	    	         	type: "post",
	    	         	url: _ctx+"/rdp/doorPermission/permissionTypes/edit4?id="+id,
	    	         	    contentType: "application/json;charset=utf-8;",
	    	         	   data:JSON.stringify({usercount:doornum}),
	    	         	  dataType:"json",//注意不是datatype哦
	    	         	    async: false,//使用同步操作，否则init方法执行时setting可能还未被赋值
	    	         	   success: function(data) {
	    	         	      //    alert("绑定门数成功！");
	    	         	  },
	    	         	 error: function(){
	    	         		  alert("绑定门失败！");
	    	              }
	    	         	 
	    	         	 }); 
	    	         	 //end 
	    	         	 
	    	         	 
			
			
		}else{
			alert("必须填完才能进行保存，不能进行类型授权操作");
		}
	  
	  
     
});





//下拉栏表框初始化初始化      
//初始化赋值
$(function () {
    //初始化时间段控件
    initList('hangwei.cnblogs.com', 'demo1');
 
 
 
});


function initList(queryParam1,selectClass, selectedDataStr) {
	//alert("时间段初始化");
    var paramData = {
        'testParam1': queryParam1
    }
    $.ajax({
        url: _ctx+"/rdp/TimeSegment/timeSeg/getData",
        type: 'get',
        data: paramData,
        async: false,
        success: function (returnData) {
        	console.log(returnData);
            var objs = $.parseJSON( JSON.stringify(returnData));
      
           
          var html = "";
           $('#datasetProperty').append("<option value='0'  checked='checked'>--任意时间有效--</option>");
            for (var i in objs) {
            	html += '<option value=' + objs[i].timeseg  + '>' + objs[i].timeseg + '</option>'
            }
            $("#datasetProperty").append(html); //渲染
        },
        error: function (e) {
            alert(e.msg);
        }
    });
}
//下拉栏表框初始化完毕    









               var   id=$("#permissionProperty").val();
         
    //第二个列表框初始化      
        //初始化赋值
      $(function () {
            //初始化
            initListBox2('hangwei.cnblogs.com', 'demo2');

            $("#showValue2").click(function () {
                alert($('[name="duallistbox_demo2"]').val());
            });
        });
        /*初始化duallistbox*/
        //queryParam1：参数
        //selectClass：select元素class属性
        //selectedDataStr：选中数据，多个以,隔开
        function initListBox2(queryParam1,selectClass, selectedDataStr) {
            var paramData = {
                'testParam1': queryParam1
            }
            $.ajax({
                url: _ctx+"/rdp/door/door/getData?id="+id,
                type: 'get',
                data: paramData,
                async: true,
                success: function (returnData) {
                    var objs = $.parseJSON( JSON.stringify(returnData));
                 //   alert("aaaa");
                  // console.log(objs);
                  
                    var json = [];
                    
                    $(objs).each(function () {
                    	  var o = document.createElement("option");
                          
                          
                          var label = this['key'];
      	                
      	                console.log(label);
      	                
      	                
                         var value = this['doorno']+","+this['doorname'];
                     
                         var  flag=this['selectedflag'];
                        
                         if(!flag){ //flase 
                         
                          $('.demo2').append('<option value="' + value + '">' + label + '</option>');
                         }else{
                      	   $('.demo2').append('<option value="' + value +'" selected="selected">' +label + '</option>');
                         }
                    });
                    
               
    	            
                    //渲染dualListbox
                    $('.' + selectClass + '').bootstrapDualListbox({
                        nonSelectedListLabel: '门列表 ',
                        selectedListLabel: ' 已选择门数：',
                        preserveSelectionOnMove: 'moved',
                        moveOnSelect: false//,
                        //nonSelectedFilter: 'ion ([7-9]|[1][0-2])'
                    });
                },
                error: function (e) {
                    alert(e.msg);
                }
            });
        } 
//第二个列表框初始化完毕
      

  function cge(){
    var sel=document.getElementById('datasetProperty');
            var sid=sel.selectedIndex;
  //  alert(sel[sid].value+'-'+sel[sid].innerHTML);
     index=sel[sid].innerHTML;
  //  alert("index="+index);
    selectvalue=sel[sid].value;
     
     
     //  alert("获取选择数值="+selectvalue);
}      


 
  
 ////列表框事件：
	   
  
  
  
  
 
	  
	 
	  
	  
	  //查询控制器
	  function   queryctl(){  
		     
		//  (" 查询控制器信息,请稍等...");
	   
	     //获取信息
	       var ctl= $("#ctl").val();
	       var zoneid= $("#hid_zoonid").val();
	       
	       var doorname= $("#doorname").val();
	     //发送ajax请求
	       $.ajax({
        url: _ctx+"/rdp/door/door/query",
        type: 'get',
        data : {ctl : ctl,zoneid:zoneid,doorname:doorname}, //参数：当前页为1
        async: false,
        success: function (data) {
        	       console.log(data);
        	      
        	       $("." + 'demo2' + "").empty();
					  
					   $('.demo2').bootstrapDualListbox('removeSelectedLabel');
					
					   $('.demo2').bootstrapDualListbox('setMoveAllLabel','', true);
					   
					   appendHtml2(data);
        	      
        },
        error: function (e) {
            (e.msg);
        }
    });
	     //结果赋值
		      }  
	  
	  
	  
    
	   var meettingid='${meetingManage.id}'; 	   
	   var ids = [];
	   var win = {};
	   //通过指定的ID重加载表格
	   function refreshGrid(ids){		   
		   $("#campusCardGrid").jqGrid('clearGridData');  //清空表格
		   $("#campusCardGrid").jqGrid('setGridParam',{
			    url:'${ctx}/rdp/user/campusCard/listByIds.htm?ids='+ids.toString()
		   }).trigger("reloadGrid");	
		   $("#members").attr("value",ids.toString());
	   }
	   //人员选择
	   var selectMember = function (){
		  var iwidth = 1000;
		  var iheight = 550;
		  var itop = (window.screen.availHeight-iheight)/2;       //获得窗口的垂直位置;
		  var ileft = (window.screen.availWidth-iwidth)/2;      //获得窗口的水平位置; 		   		   
		  win = window.open(
				"${ctx}/rdp/popup/tree.htm", 
				"人員選擇", 
				"status=no,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,top="+itop+",left="+ileft+",width="+iwidth+",height="+iheight
		   );   
	   }
	   //彈窗回調
	   var initIds = function (addIds){
		   //合并
		   ids = ids.concat(addIds);
		   //去重
		   ids = ids.filter((item, index, self) => self.indexOf(item) ==index);		   
		   win.close();
		   refreshGrid(ids);
	   }
	   //移除
       function removeGridData(id){
    	   for (var i = 0; i < ids.length; i++) { 
    		   if (ids[i] == id) {
    			   ids.splice(i, 1); 
    			   break;
    		   }    		
    	   }
    	   refreshGrid(ids);
       }
	   //模板下載
       function downloadExcel() {    	    
      	    window.location.href="${ctx}/rdp/popup/downloadExcel";
       }
       //上传
       function fileClick(){
      	  $("#upfile").click();
       }
       
	   //初始化表单
       $(function(){
    	   gridInit();
    	   //绑定上传事件
    	   $("#upfile").change(importExp);
       });
       //上传事件
       function importExp() {
    		//var index = layer.load(1, {time: 20*1000});  // 调用加载层
    		var formData = new FormData();
    		var name = $("#upfile").val();
    		formData.append("name",name);
    		formData.append("file",$("#upfile")[0].files[0]);
    	 
    		$.ajax({
    			url : '${ctx}/rdp/popup/uploadExcel',
    			type : 'POST',
    			data : formData,
    			//ajax2.0可以不用设置请求头，但是jq帮我们自动设置了，这样的话需要我们自己取消掉
    			contentType:false,
    			//取消帮我们格式化数据，是什么就是什么
    			processData:false,
    			success : function(data) {
    				//关闭 加载层
    				//layer.close(index);
    				if(data.status==true){
    				//layer.msg("导入成功");
    				   var newAddIds = data.ids;
    				   ids = ids.concat(newAddIds);
    				   ids = ids.filter((item, index, self) => self.indexOf(item) ==index);		   
    				   refreshGrid(ids);    				 
    				}else{
    				//layer.msg("导入失败");
    				}
    		    }
    		});
    	 
    		$("#upfile").val("");
    	}
       
                   
       function gridInit(){
    	   $("#campusCardGrid").GridList(
					{
						url :  '${ctx}/rdp/user/campusCard/permissionUserList.htm?meetingId=',
						pager :"#campusCardPager",
						colNames: ['ID','姓名','工号','电话','部门代码','部门名称','性别','操作'],
				        colModel: [{
		                 	   name:'id',
		                	   index: 'ID',
		                	   hidden:true,
		                	   key:true
		                	},		                	
		                	{
		                 	   name:'username',
		                	   index: 't.USERNAME'
		                	},
                            {
		                 	   name:'usernum',
		                	   index: 't.USERNUM'
		                	},	
		                	{
		                 	   name:'phone',
		                	   index: 't.PHONE'
		                	 },  
		                	 {
		                 	   name:'orgcode',
		                	   index: 't.ORGCODE',
		                	   hidden:true,
                             },   
		                	 {
			                 	   name:'orgname',
			                 	   index: 'd.ORGNAME',
			                	   sortable: false 
                             }, 
                             {
		                 	   name:'sex',
		                	   index: 'SEX',
		                	   formatter:function(cellValue, options, rowObject){
		                		   if(cellValue==1){
		                			   return '男';
		                		   }else{
		                			   return '女';
		                		   }
		                	   }
                             }, 
                             {
									name : '__manage',
									width : 30,
									sortable:false,
									//classes:'rowOps',
									//formatter : 'manage',
									//formatoptions :[{
									//	label:'删除',
									//	classes:'btn btn-primary fa fa-remove',
									//    action: ''
									//}]
									formatter:function(cellValue, options, rowObject){
										var id = options['rowId'];
										var html = "<a  href='javascript:void(0);' onclick = 'removeGridData("+id+")'>删除<a>"
										return html;

				                	}
		                	 
		                	        
							} ],

							//点击行，进行单选
					        //要求：editable:false
					        onSelectRow: function (rowId, status, e) {
					        	
					            var ids=$('#campusCardGrid').jqGrid('getGridParam','selarrrow');
								selectIds = ids;

					        },
							//点击题头的checkbox按钮，一键多选
					        onSelectAll:function(rowids,statue){
					            selectIds = rowids;
					        }						
	
					});
       }


	</script>
    

</body>




	
</html>
